<?php

namespace app\admin\controller;

use app\common\model\Users;
use think\Controller;
use think\Request;

class Account extends Controller
{

    // 登录页面
    public function login(Request $request)
    {
        if ( $request->isPost() ){
            $post = trimAll($request->post());

            if ( !$user = Users::where(['username'=>$post['username']])->find() ){
                createLog('没有此用户：' . $post['username']);
                error('没有此用户');
            }

            if ( $user['password'] !== sha1_password($post['password']) ){
                createLog('密码错误：' . $post['username'] . ' / ' . $post['password']);
                error('密码错误');
            }

            if ( $user['admin'] != 1 && $user['admin'] != 4){
                error('没有管理权限');
            }

            if ( $user['status'] != 1 ){
                error('账户已禁用');
            }

            unset($user['password']);
            session('admin', $user);

            createLog('登录成功');
            success('登录成功');
        } else {
            return view();
        }
    }



    // 安全退出
    public function logout()
    {
        session('admin', null);
        cookie('authcode', null);
        session(null);
        cookie(null);
        return json(array('code'=>1, 'msg'=>'已安全退出'));
        $this->error('已安全退出...', url('admin/account/login'));
    }

}
